<{extend name="Public:public" /}>

<{block name="title"}>渲染函数 & JSX<{/block}>

<{block name="content"}> 
<div class="content guide with-sidebar render-function-guide">
  
    
      <div id="ad">
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CKYIK2QU&placement=vuejsorg"
    id="_carbonads_js">
  </script> -->
  <a href="https://studio.qcloud.coding.net/campaign/favorite-plugins/?utm_source=vue.js&amp;utm_content=ad1116" target="_blank">
    <img src="/images/coding-net-ad.jpg">
  </a>
</div>

    
  
  
    <h1>渲染函数 &amp; JSX</h1>
  
  
    <h2 id="基础"><a href="#基础" class="headerlink" title="基础" data-scroll="">基础</a></h2><p>Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML。然而在一些场景中，你真的需要 JavaScript 的完全编程的能力，这时你可以用 <strong>render 函数</strong>，它比 template 更接近编译器。</p>
<p>让我们深入一个简单的例子，这个例子里 <code>render</code> 函数很实用。假设我们要生成锚点标题 (anchored headings)：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">a</span> <span class="attr">name</span>=<span class="string">"hello-world"</span> <span class="attr">href</span>=<span class="string">"#hello-world"</span>&gt;</span></span><br><span class="line">    Hello world!</span><br><span class="line">  <span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>对于上面的 HTML，我们决定这样定义组件接口：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">anchored-heading</span> <span class="attr">:level</span>=<span class="string">"1"</span>&gt;</span>Hello world!<span class="tag">&lt;/<span class="name">anchored-heading</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>当我们开始写一个只能通过 <code>level</code> prop 动态生成 heading 标签的组件时，你可能很快想到这样实现：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/x-template"</span> <span class="attr">id</span>=<span class="string">"anchored-heading-template"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">  &lt;h1 v-<span class="keyword">if</span>=<span class="string">"level === 1"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="javascript">  &lt;h2 v-<span class="keyword">else</span>-<span class="keyword">if</span>=<span class="string">"level === 2"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="javascript">  &lt;h3 v-<span class="keyword">else</span>-<span class="keyword">if</span>=<span class="string">"level === 3"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line"><span class="javascript">  &lt;h4 v-<span class="keyword">else</span>-<span class="keyword">if</span>=<span class="string">"level === 4"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span></span><br><span class="line"><span class="javascript">  &lt;h5 v-<span class="keyword">else</span>-<span class="keyword">if</span>=<span class="string">"level === 5"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h5</span>&gt;</span></span></span><br><span class="line"><span class="javascript">  &lt;h6 v-<span class="keyword">else</span>-<span class="keyword">if</span>=<span class="string">"level === 6"</span>&gt;</span></span><br><span class="line"><span class="javascript">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">h6</span>&gt;</span></span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'anchored-heading'</span>, {</span><br><span class="line">  template: <span class="string">'#anchored-heading-template'</span>,</span><br><span class="line">  props: {</span><br><span class="line">    level: {</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>在这种场景中使用 template 并不是最好的选择：首先代码冗长，为了在不同级别的标题中插入锚点元素，我们需要重复地使用 <code>&lt;slot&gt;&lt;/slot&gt;</code>。</p>
<p>虽然模板在大多数组件中都非常好用，但是在这里它就不是很简洁的了。那么，我们来尝试使用 <code>render</code> 函数重写上面的例子：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'anchored-heading'</span>, {</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> createElement(</span><br><span class="line">      <span class="string">'h'</span> + <span class="keyword">this</span>.level,   <span class="comment">// 标签名称</span></span><br><span class="line">      <span class="keyword">this</span>.$slots.default <span class="comment">// 子元素数组</span></span><br><span class="line">    )</span><br><span class="line">  },</span><br><span class="line">  props: {</span><br><span class="line">    level: {</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>简单清晰很多！简单来说，这样代码精简很多，但是需要非常熟悉 Vue 的实例属性。在这个例子中，你需要知道，向组件中传递不带 <code>slot</code> 特性的子元素时，比如 <code>anchored-heading</code> 中的 <code>Hello world!</code>，这些子元素被存储在组件实例中的 <code>$slots.default</code> 中。如果你还不了解，<strong>在深入 render 函数之前推荐阅读<a href="../api/#实例属性">实例属性 API</a>。</strong></p>
<h2 id="节点、树以及虚拟-DOM"><a href="#节点、树以及虚拟-DOM" class="headerlink" title="节点、树以及虚拟 DOM" data-scroll="">节点、树以及虚拟 DOM</a></h2><p>在深入渲染函数之前，了解一些浏览器的工作原理是很重要的。以下面这段 HTML 为例：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>My title<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  Some text content</span><br><span class="line">  <span class="comment">&lt;!-- <span class="doctag">TODO:</span> 添加标签行 --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>当浏览器读到这些代码时，它会建立一个<a href="https://javascript.info/dom-nodes" target="_blank" rel="noopener">“DOM 节点”树</a>来保持追踪，如同你会画一张家谱树来追踪家庭成员的发展一样。</p>
<p>HTML 的 DOM 节点树如下图所示：</p>
<p><img src="/images/dom-tree.png" alt="DOM Tree Visualization"></p>
<p>每个元素都是一个节点。每片文字也是一个节点。甚至注释也都是节点。一个节点就是页面的一个部分。就像家谱树一样，每个节点都可以有孩子节点 (也就是说每个部分可以包含其它的一些部分)。</p>
<p>高效的更新所有这些节点会是比较困难的，不过所幸你不必再手动完成这个工作了。你只需要告诉 Vue 你希望页面上的 HTML 是什么，这可以是在一个模板里：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>{{ blogTitle }}<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>或者一个渲染函数里：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'h1'</span>, <span class="keyword">this</span>.blogTitle)</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>在这两种情况下，Vue 都会自动保持页面的更新，即便 <code>blogTitle</code> 发生了改变。</p>
<h3 id="虚拟-DOM"><a href="#虚拟-DOM" class="headerlink" title="虚拟 DOM" data-scroll="">虚拟 DOM</a></h3><p>Vue 通过建立一个<strong>虚拟 DOM</strong> 对真实 DOM 发生的变化保持追踪。请仔细看这行代码：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">return</span> createElement(<span class="string">'h1'</span>, <span class="keyword">this</span>.blogTitle)</span><br></pre></td></tr></tbody></table></figure>
<p><code>createElement</code> 到底会返回什么呢？其实不是一个<em>实际的</em> DOM 元素。它更准确的名字可能是 <code>createNodeDescription</code>，因为它所包含的信息会告诉 Vue 页面上需要渲染什么样的节点，及其子节点。我们把这样的节点描述为“虚拟节点 (Virtual Node)”，也常简写它为“VNode”。“虚拟 DOM”是我们对由 Vue 组件树建立起来的整个 VNode 树的称呼。</p>
<h2 id="createElement-参数"><a href="#createElement-参数" class="headerlink" title="createElement 参数" data-scroll=""><code>createElement</code> 参数</a></h2><p>接下来你需要熟悉的是如何在 <code>createElement</code> 函数中生成模板。这里是 <code>createElement</code> 接受的参数：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">// @returns {VNode}</span></span><br><span class="line">createElement(</span><br><span class="line">  <span class="comment">// {String | Object | Function}</span></span><br><span class="line"> &nbsp;<span class="comment">// 一个 HTML 标签字符串，组件选项对象，或者</span></span><br><span class="line">  <span class="comment">// 解析上述任何一种的一个 async 异步函数。必需参数。</span></span><br><span class="line"> &nbsp;<span class="string">'div'</span>,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// {Object}</span></span><br><span class="line"> &nbsp;<span class="comment">// 一个包含模板相关属性的数据对象</span></span><br><span class="line">  <span class="comment">// 你可以在 template 中使用这些特性。可选参数。</span></span><br><span class="line">  {</span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// (详情见下一节)</span></span><br><span class="line">  },</span><br><span class="line"></span><br><span class="line">  <span class="comment">// {String | Array}</span></span><br><span class="line"> &nbsp;<span class="comment">// 子虚拟节点 (VNodes)，由 `createElement()` 构建而成，</span></span><br><span class="line">  <span class="comment">// 也可以使用字符串来生成“文本虚拟节点”。可选参数。</span></span><br><span class="line">  [</span><br><span class="line">    <span class="string">'先写一些文字'</span>,</span><br><span class="line">    createElement(<span class="string">'h1'</span>, <span class="string">'一则头条'</span>),</span><br><span class="line">    createElement(MyComponent, {</span><br><span class="line">      props: {</span><br><span class="line">        someProp: <span class="string">'foobar'</span></span><br><span class="line">      }</span><br><span class="line">    })</span><br><span class="line">  ]</span><br><span class="line">)</span><br></pre></td></tr></tbody></table></figure>
<h3 id="深入-data-对象"><a href="#深入-data-对象" class="headerlink" title="深入 data 对象" data-scroll="">深入 data 对象</a></h3><p>有一点要注意：正如在模板语法中，<code>v-bind:class</code> 和 <code>v-bind:style</code>，会被特别对待一样，在 VNode 数据对象中，下列属性名是级别最高的字段。该对象也允许你绑定普通的 HTML 特性，就像 DOM 属性一样，比如 <code>innerHTML</code> (这会取代 <code>v-html</code> 指令)。</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">{</span><br><span class="line">  <span class="comment">// 和`v-bind:class`一样的 API</span></span><br><span class="line">  <span class="comment">// 接收一个字符串、对象或字符串和对象组成的数组</span></span><br><span class="line">  <span class="string">'class'</span>: {</span><br><span class="line">    foo: <span class="literal">true</span>,</span><br><span class="line">    bar: <span class="literal">false</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// 和`v-bind:style`一样的 API</span></span><br><span class="line">  <span class="comment">// 接收一个字符串、对象或对象组成的数组</span></span><br><span class="line">  style: {</span><br><span class="line">    color: <span class="string">'red'</span>,</span><br><span class="line">    fontSize: <span class="string">'14px'</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// 普通的 HTML 特性</span></span><br><span class="line">  attrs: {</span><br><span class="line">    id: <span class="string">'foo'</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// 组件 props</span></span><br><span class="line">  props: {</span><br><span class="line">    myProp: <span class="string">'bar'</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// DOM 属性</span></span><br><span class="line">  domProps: {</span><br><span class="line">    innerHTML: <span class="string">'baz'</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// 事件监听器基于 `on`</span></span><br><span class="line">  <span class="comment">// 所以不再支持如 `v-on:keyup.enter` 修饰器</span></span><br><span class="line">  <span class="comment">// 需要手动匹配 keyCode。</span></span><br><span class="line">  on: {</span><br><span class="line">    click: <span class="keyword">this</span>.clickHandler</span><br><span class="line">  },</span><br><span class="line"> &nbsp;<span class="comment">// 仅用于组件，用于监听原生事件，而不是组件内部使用</span></span><br><span class="line">  <span class="comment">// `vm.$emit` 触发的事件。</span></span><br><span class="line">  nativeOn: {</span><br><span class="line">    click: <span class="keyword">this</span>.nativeClickHandler</span><br><span class="line">  },</span><br><span class="line"> &nbsp;<span class="comment">// 自定义指令。注意，你无法对 `binding` 中的 `oldValue`</span></span><br><span class="line">  <span class="comment">// 赋值，因为 Vue 已经自动为你进行了同步。</span></span><br><span class="line"> &nbsp;directives: [</span><br><span class="line">    {</span><br><span class="line">      name: <span class="string">'my-custom-directive'</span>,</span><br><span class="line">      value: <span class="string">'2'</span>,</span><br><span class="line">      expression: <span class="string">'1 + 1'</span>,</span><br><span class="line">      arg: <span class="string">'foo'</span>,</span><br><span class="line">      modifiers: {</span><br><span class="line">        bar: <span class="literal">true</span></span><br><span class="line">      }</span><br><span class="line">    }</span><br><span class="line">  ],</span><br><span class="line"> &nbsp;<span class="comment">// 作用域插槽格式</span></span><br><span class="line">  <span class="comment">// { name: props =&gt; VNode | Array&lt;VNode&gt; }</span></span><br><span class="line">  scopedSlots: {</span><br><span class="line">    <span class="keyword">default</span>: <span class="function"><span class="params">props</span> =&gt;</span> createElement(<span class="string">'span'</span>, props.text)</span><br><span class="line">  },</span><br><span class="line"> &nbsp;<span class="comment">// 如果组件是其他组件的子组件，需为插槽指定名称</span></span><br><span class="line"> &nbsp;slot: <span class="string">'name-of-slot'</span>,</span><br><span class="line">  <span class="comment">// 其他特殊顶层属性</span></span><br><span class="line">  key: <span class="string">'myKey'</span>,</span><br><span class="line">  ref: <span class="string">'myRef'</span>,</span><br><span class="line">  <span class="comment">// 如果你在渲染函数中向多个元素都应用了相同的 ref 名，</span></span><br><span class="line">  <span class="comment">// 那么 `$refs.myRef` 会变成一个数组。</span></span><br><span class="line">  refInFor: <span class="literal">true</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="完整示例"><a href="#完整示例" class="headerlink" title="完整示例" data-scroll="">完整示例</a></h3><p>有了这些知识，我们现在可以完成我们最开始想实现的组件：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> getChildrenTextContent = <span class="function"><span class="keyword">function</span> (<span class="params">children</span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> children.map(<span class="function"><span class="keyword">function</span> (<span class="params">node</span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> node.children</span><br><span class="line">      ? getChildrenTextContent(node.children)</span><br><span class="line">      : node.text</span><br><span class="line">  }).join(<span class="string">''</span>)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Vue.component(<span class="string">'anchored-heading'</span>, {</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 创建 kebab-case 风格的ID</span></span><br><span class="line"> &nbsp;  <span class="keyword">var</span> headingId = getChildrenTextContent(<span class="keyword">this</span>.$slots.default)</span><br><span class="line">      .toLowerCase()</span><br><span class="line">      .replace(<span class="regexp">/\W+/g</span>, <span class="string">'-'</span>)</span><br><span class="line">      .replace(<span class="regexp">/(^\-|\-$)/g</span>, <span class="string">''</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> createElement(</span><br><span class="line">      <span class="string">'h'</span> + <span class="keyword">this</span>.level,</span><br><span class="line">      [</span><br><span class="line">        createElement(<span class="string">'a'</span>, {</span><br><span class="line">          attrs: {</span><br><span class="line">            name: headingId,</span><br><span class="line">            href: <span class="string">'#'</span> + headingId</span><br><span class="line">          }</span><br><span class="line">        }, <span class="keyword">this</span>.$slots.default)</span><br><span class="line">      ]</span><br><span class="line">    )</span><br><span class="line">  },</span><br><span class="line">  props: {</span><br><span class="line">    level: {</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<h3 id="约束"><a href="#约束" class="headerlink" title="约束" data-scroll="">约束</a></h3><h4 id="VNodes-必须唯一"><a href="#VNodes-必须唯一" class="headerlink" title="VNodes 必须唯一"></a>VNodes 必须唯一</h4><p>组件树中的所有 VNodes 必须是唯一的。这意味着，下面的 render function 是无效的：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">var</span> myParagraphVNode = createElement(<span class="string">'p'</span>, <span class="string">'hi'</span>)</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'div'</span>, [</span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 错误-重复的 VNodes</span></span><br><span class="line"> &nbsp; &nbsp;myParagraphVNode, myParagraphVNode</span><br><span class="line">  ])</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>如果你真的需要重复很多次的元素/组件，你可以使用工厂函数来实现。例如，下面这个例子 render 函数完美有效地渲染了 20 个相同的段落：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'div'</span>,</span><br><span class="line">    <span class="built_in">Array</span>.apply(<span class="literal">null</span>, { <span class="attr">length</span>: <span class="number">20</span> }).map(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">      <span class="keyword">return</span> createElement(<span class="string">'p'</span>, <span class="string">'hi'</span>)</span><br><span class="line">    })</span><br><span class="line">  )</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="使用-JavaScript-代替模板功能"><a href="#使用-JavaScript-代替模板功能" class="headerlink" title="使用 JavaScript 代替模板功能" data-scroll="">使用 JavaScript 代替模板功能</a></h2><h3 id="v-if-和-v-for"><a href="#v-if-和-v-for" class="headerlink" title="v-if 和 v-for" data-scroll=""><code>v-if</code> 和 <code>v-for</code></a></h3><p>只要在原生的 JavaScript 中可以轻松完成的操作，Vue 的 render 函数就不会提供专有的替代方法。比如，在 template 中使用的 <code>v-if</code> 和 <code>v-for</code>：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">v-if</span>=<span class="string">"items.length"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">"item in items"</span>&gt;</span>{{ item.name }}<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-else</span>&gt;</span>No items found.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>这些都会在 render 函数中被 JavaScript 的 <code>if</code>/<code>else</code> 和 <code>map</code> 重写：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'items'</span>],</span><br><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">this</span>.items.length) {</span><br><span class="line">    <span class="keyword">return</span> createElement(<span class="string">'ul'</span>, <span class="keyword">this</span>.items.map(<span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</span><br><span class="line">      <span class="keyword">return</span> createElement(<span class="string">'li'</span>, item.name)</span><br><span class="line">    }))</span><br><span class="line">  } <span class="keyword">else</span> {</span><br><span class="line">    <span class="keyword">return</span> createElement(<span class="string">'p'</span>, <span class="string">'No items found.'</span>)</span><br><span class="line">  }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="v-model"><a href="#v-model" class="headerlink" title="v-model" data-scroll=""><code>v-model</code></a></h3><p>render 函数中没有与 <code>v-model</code> 的直接对应 - 你必须自己实现相应的逻辑：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'value'</span>],</span><br><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">var</span> self = <span class="keyword">this</span></span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'input'</span>, {</span><br><span class="line">    domProps: {</span><br><span class="line">      value: self.value</span><br><span class="line">    },</span><br><span class="line">    on: {</span><br><span class="line">      input: <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>{</span><br><span class="line">        self.$emit(<span class="string">'input'</span>, event.target.value)</span><br><span class="line">      }</span><br><span class="line">    }</span><br><span class="line">  })</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>这就是深入底层的代价，但与 <code>v-model</code> 相比，这可以让你更好地控制交互细节。</p>
<h3 id="事件-amp-按键修饰符"><a href="#事件-amp-按键修饰符" class="headerlink" title="事件 &amp; 按键修饰符" data-scroll="">事件 &amp; 按键修饰符</a></h3><p>对于 <code>.passive</code>、<code>.capture</code> 和 <code>.once</code>事件修饰符, Vue 提供了相应的前缀可以用于 <code>on</code>：</p>
<table>
<thead>
<tr>
<th>Modifier(s)</th>
<th>Prefix</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>.passive</code></td>
<td><code>&amp;</code></td>
</tr>
<tr>
<td><code>.capture</code></td>
<td><code>!</code></td>
</tr>
<tr>
<td><code>.once</code></td>
<td><code>~</code></td>
</tr>
<tr>
<td><code>.capture.once</code> or<br><code>.once.capture</code></td>
<td><code>~!</code></td>
</tr>
</tbody>
</table>
<p>例如:</p>
<figure class="highlight javascript"><table><tbody><tr><td class="code"><pre><span class="line">on: {</span><br><span class="line">  <span class="string">'!click'</span>: <span class="keyword">this</span>.doThisInCapturingMode,</span><br><span class="line">  <span class="string">'~keyup'</span>: <span class="keyword">this</span>.doThisOnce,</span><br><span class="line">  <span class="string">'~!mouseover'</span>: <span class="keyword">this</span>.doThisOnceInCapturingMode</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>对于其他的修饰符，前缀不是很重要，因为你可以在事件处理函数中使用事件方法：</p>
<table>
<thead>
<tr>
<th>Modifier(s)</th>
<th>Equivalent in Handler</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>.stop</code></td>
<td><code>event.stopPropagation()</code></td>
</tr>
<tr>
<td><code>.prevent</code></td>
<td><code>event.preventDefault()</code></td>
</tr>
<tr>
<td><code>.self</code></td>
<td><code>if (event.target !== event.currentTarget) return</code></td>
</tr>
<tr>
<td>Keys:<br><code>.enter</code>, <code>.13</code></td>
<td><code>if (event.keyCode !== 13) return</code> (change <code>13</code> to <a href="http://keycode.info/" target="_blank" rel="noopener">another key code</a> for other key modifiers)</td>
</tr>
<tr>
<td>Modifiers Keys:<br><code>.ctrl</code>, <code>.alt</code>, <code>.shift</code>, <code>.meta</code></td>
<td><code>if (!event.ctrlKey) return</code> (change <code>ctrlKey</code> to <code>altKey</code>, <code>shiftKey</code>, or <code>metaKey</code>, respectively)</td>
</tr>
</tbody>
</table>
<p>这里是一个使用所有修饰符的例子：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="code"><pre><span class="line">on: {</span><br><span class="line">  keyup: <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>{</span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 如果触发事件的元素不是事件绑定的元素</span></span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 则返回</span></span><br><span class="line"> &nbsp; &nbsp;<span class="keyword">if</span> (event.target !== event.currentTarget) <span class="keyword">return</span></span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 如果按下去的不是 enter 键或者</span></span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 没有同时按下 shift 键</span></span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 则返回</span></span><br><span class="line"> &nbsp; &nbsp;<span class="keyword">if</span> (!event.shiftKey || event.keyCode !== <span class="number">13</span>) <span class="keyword">return</span></span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 阻止&nbsp;事件冒泡</span></span><br><span class="line"> &nbsp;  event.stopPropagation()</span><br><span class="line"> &nbsp; &nbsp;<span class="comment">// 阻止该元素默认的 keyup 事件</span></span><br><span class="line"> &nbsp; &nbsp;event.preventDefault()</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="插槽"><a href="#插槽" class="headerlink" title="插槽" data-scroll="">插槽</a></h3><p>你可以通过 <a href="../api/#vm-slots"><code>this.$slots</code></a> 访问静态插槽的内容，得到的是一个 VNodes 数组：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="comment">// `&lt;div&gt;&lt;slot&gt;&lt;/slot&gt;&lt;/div&gt;`</span></span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'div'</span>, <span class="keyword">this</span>.$slots.default)</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>也可以通过 <a href="../api/#vm-scopedSlots"><code>this.$scopedSlots</code></a> 访问作用域插槽，得到的是一个返回 VNodes 的函数：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'message'</span>],</span><br><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="comment">// `&lt;div&gt;&lt;slot :text="message"&gt;&lt;/slot&gt;&lt;/div&gt;`</span></span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'div'</span>, [</span><br><span class="line">    <span class="keyword">this</span>.$scopedSlots.default({</span><br><span class="line">      text: <span class="keyword">this</span>.message</span><br><span class="line">    })</span><br><span class="line">  ])</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>如果要用渲染函数向子组件中传递作用域插槽，可以利用 VNode 数据对象中的 <code>scopedSlots</code> 域：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> createElement(<span class="string">'div'</span>, [</span><br><span class="line">    createElement(<span class="string">'child'</span>, {</span><br><span class="line">      <span class="comment">// 在数据对象中传递 `scopedSlots`</span></span><br><span class="line">      <span class="comment">// 格式：{ name: props =&gt; VNode | Array&lt;VNode&gt; }</span></span><br><span class="line">      scopedSlots: {</span><br><span class="line">        <span class="keyword">default</span>: <span class="function"><span class="keyword">function</span> (<span class="params">props</span>) </span>{</span><br><span class="line">          <span class="keyword">return</span> createElement(<span class="string">'span'</span>, props.text)</span><br><span class="line">        }</span><br><span class="line">      }</span><br><span class="line">    })</span><br><span class="line">  ])</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="JSX"><a href="#JSX" class="headerlink" title="JSX" data-scroll="">JSX</a></h2><p>如果你写了很多 <code>render</code> 函数，可能会觉得下面这样的代码写起来很痛苦：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">createElement(</span><br><span class="line">  <span class="string">'anchored-heading'</span>, {</span><br><span class="line">    props: {</span><br><span class="line">      level: <span class="number">1</span></span><br><span class="line">    }</span><br><span class="line">  }, [</span><br><span class="line">    createElement(<span class="string">'span'</span>, <span class="string">'Hello'</span>),</span><br><span class="line">    <span class="string">' world!'</span></span><br><span class="line">  ]</span><br><span class="line">)</span><br></pre></td></tr></tbody></table></figure>
<p>特别是模板如此简单的情况下：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">anchored-heading</span> <span class="attr">:level</span>=<span class="string">"1"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span>&gt;</span>Hello<span class="tag">&lt;/<span class="name">span</span>&gt;</span> world!</span><br><span class="line"><span class="tag">&lt;/<span class="name">anchored-heading</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>这就是为什么会有一个 <a href="https://github.com/vuejs/babel-plugin-transform-vue-jsx" target="_blank" rel="noopener">Babel 插件</a>，用于在 Vue 中使用 JSX 语法，它可以让我们回到更接近于模板的语法上。</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> AnchoredHeading <span class="keyword">from</span> <span class="string">'./AnchoredHeading.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line">  el: <span class="string">'#demo'</span>,</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">h</span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;AnchoredHeading level={<span class="number">1</span>}&gt;</span><br><span class="line">        &lt;span&gt;Hello&lt;<span class="regexp">/span&gt; world!</span></span><br><span class="line"><span class="regexp">      &lt;/</span>AnchoredHeading&gt;</span><br><span class="line">    )</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p class="tip">将 <code>h</code> 作为 <code>createElement</code> 的别名是 Vue 生态系统中的一个通用惯例，实际上也是 JSX 所要求的，如果在作用域中 <code>h</code> 失去作用，在应用中会触发报错。</p>

<p>更多关于 JSX 映射到 JavaScript，阅读 <a href="https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage" target="_blank" rel="noopener">使用文档</a>。</p>
<h2 id="函数式组件"><a href="#函数式组件" class="headerlink" title="函数式组件" data-scroll="">函数式组件</a></h2><p>之前创建的锚点标题组件是比较简单，没有管理或者监听任何传递给他的状态，也没有生命周期方法。它只是一个接收参数的函数。<br>在这个例子中，我们标记组件为 <code>functional</code>，这意味它是无状态 (没有<a href="../api/#选项-数据">响应式数据</a>)，无实例 (没有 <code>this</code> 上下文)。<br>一个<strong>函数式组件</strong>就像这样：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'my-component'</span>, {</span><br><span class="line">  functional: <span class="literal">true</span>,</span><br><span class="line">  <span class="comment">// Props 可选</span></span><br><span class="line">  props: {</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  },</span><br><span class="line">  <span class="comment">// 为了弥补缺少的实例</span></span><br><span class="line">  <span class="comment">// 提供第二个参数作为上下文</span></span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement, context</span>) </span>{</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<blockquote>
<p>注意：在 2.3.0 之前的版本中，如果一个函数式组件想要接受 props，则 <code>props</code> 选项是必须的。在 2.3.0 或以上的版本中，你可以省略 <code>props</code> 选项，所有组件上的特性都会被自动解析为 props。</p>
</blockquote>
<p>在 2.5.0 及以上版本中，如果你使用了<a href="single-file-components.html">单文件组件</a>，那么基于模板的函数式组件可以这样声明：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">functional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>组件需要的一切都是通过上下文传递，包括：</p>
<ul>
<li><code>props</code>：提供所有 prop 的对象</li>
<li><code>children</code>: VNode 子节点的数组</li>
<li><code>slots</code>: 返回所有插槽的对象的函数</li>
<li><code>data</code>：传递给组件的<a href="#深入-data-对象">数据对象</a>，作为 <code>createElement</code> 的第二个参数传入组件 </li>
<li><code>parent</code>：对父组件的引用</li>
<li><code>listeners</code>: (2.3.0+) 一个包含了所有在父组件上注册的事件侦听器的对象。这只是一个指向 <code>data.on</code> 的别名。</li>
<li><code>injections</code>: (2.3.0+) 如果使用了 <a href="../api/#provide-inject"><code>inject</code></a> 选项，则该对象包含了应当被注入的属性。</li>
</ul>
<p>在添加 <code>functional: true</code> 之后，锚点标题组件的 render 函数之间简单更新增加 <code>context</code> 参数，<code>this.$slots.default</code> 更新为 <code>context.children</code>，之后<code>this.level</code> 更新为 <code>context.props.level</code>。</p>
<p>因为函数式组件只是一个函数，所以渲染开销也低很多。然而，对持久化实例的缺乏也意味着函数式组件不会出现在 <a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener">Vue devtools</a> 的组件树里。</p>
<p>在作为包装组件时它们也同样非常有用，比如，当你需要做这些时：</p>
<ul>
<li>程序化地在多个组件中选择一个</li>
<li>在将 children, props, data 传递给子组件之前操作它们。</li>
</ul>
<p>下面是一个依赖传入 props 的值的 <code>smart-list</code> 组件例子，它能代表更多具体的组件：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> EmptyList = { <span class="comment">/* ... */</span> }</span><br><span class="line"><span class="keyword">var</span> TableList = { <span class="comment">/* ... */</span> }</span><br><span class="line"><span class="keyword">var</span> OrderedList = { <span class="comment">/* ... */</span> }</span><br><span class="line"><span class="keyword">var</span> UnorderedList = { <span class="comment">/* ... */</span> }</span><br><span class="line"></span><br><span class="line">Vue.component(<span class="string">'smart-list'</span>, {</span><br><span class="line">  functional: <span class="literal">true</span>,</span><br><span class="line">  props: {</span><br><span class="line">    items: {</span><br><span class="line">      type: <span class="built_in">Array</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    },</span><br><span class="line">    isOrdered: <span class="built_in">Boolean</span></span><br><span class="line">  },</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement, context</span>) </span>{</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">appropriateListComponent</span> (<span class="params"></span>) </span>{</span><br><span class="line">      <span class="keyword">var</span> items = context.props.items</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (items.length === <span class="number">0</span>)           <span class="keyword">return</span> EmptyList</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> items[<span class="number">0</span>] === <span class="string">'object'</span>) <span class="keyword">return</span> TableList</span><br><span class="line">      <span class="keyword">if</span> (context.props.isOrdered)      <span class="keyword">return</span> OrderedList</span><br><span class="line"></span><br><span class="line">      <span class="keyword">return</span> UnorderedList</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> createElement(</span><br><span class="line">      appropriateListComponent(),</span><br><span class="line">      context.data,</span><br><span class="line">      context.children</span><br><span class="line">    )</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<h3 id="向子元素或子组件传递特性和事件"><a href="#向子元素或子组件传递特性和事件" class="headerlink" title="向子元素或子组件传递特性和事件" data-scroll="">向子元素或子组件传递特性和事件</a></h3><p>在普通组件中，没有被定义为 prop 的特性会自动添加到组件的根元素上，将现有的同名特性替换或与其<a href="class-and-style.html">智能合并</a>。</p>
<p>然而函数式组件要求你显式定义该行为：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'my-functional-button'</span>, {</span><br><span class="line">  functional: <span class="literal">true</span>,</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement, context</span>) </span>{</span><br><span class="line">    <span class="comment">// 完全透明的传入任何特性、事件监听器、子结点等。</span></span><br><span class="line">    <span class="keyword">return</span> createElement(<span class="string">'button'</span>, context.data, context.children)</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>向 <code>createElement</code> 通过传入 <code>context.data</code> 作为第二个参数，我们就把 <code>my-functional-button</code> 上面所有的特性和事件监听器都传递下去了。事实上这是非常透明的，那些事件甚至并不要求 <code>.native</code> 修饰符。</p>
<p>如果你使用基于模板的函数式组件，那么你还需要手动添加特性和监听器。因为我们可以访问到其独立的上下文内容，所以我们可以使用 <code>data.attrs</code> 传递任何 HTML 特性，也可以使用 <code>listeners</code> <em>(即 <code>data.on</code> 的别名)</em> 传递任何事件监听器。</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">functional</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span></span></span><br><span class="line"><span class="tag">    <span class="attr">class</span>=<span class="string">"btn btn-primary"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">v-bind</span>=<span class="string">"data.attrs"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">v-on</span>=<span class="string">"listeners"</span></span></span><br><span class="line"><span class="tag">  &gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">slot</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="slots-和-children-对比"><a href="#slots-和-children-对比" class="headerlink" title="slots() 和 children 对比" data-scroll=""><code>slots()</code> 和 <code>children</code> 对比</a></h3><p>你可能想知道为什么同时需要 <code>slots()</code> 和 <code>children</code>。<code>slots().default</code> 不是和 <code>children</code> 类似的吗？在一些场景中，是这样，但是如果是函数式组件和下面这样的 children 呢？</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">my-functional-component</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">slot</span>=<span class="string">"foo"</span>&gt;</span></span><br><span class="line">    first</span><br><span class="line">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>second<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">my-functional-component</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>对于这个组件，<code>children</code> 会给你两个段落标签，而 <code>slots().default</code> 只会传递第二个匿名段落标签，<code>slots().foo</code> 会传递第一个具名段落标签。同时拥有 <code>children</code> 和 <code>slots()</code> ，因此你可以选择让组件通过 <code>slot()</code> 系统分发或者简单的通过 <code>children</code> 接收，让其他组件去处理。</p>
<h2 id="模板编译"><a href="#模板编译" class="headerlink" title="模板编译" data-scroll="">模板编译</a></h2><p>你可能有兴趣知道，Vue 的模板实际是编译成了 render 函数。这是一个实现细节，通常不需要关心，但如果你想看看模板的功能是怎样被编译的，你会发现会非常有趣。下面是一个使用 <code>Vue.compile</code> 来实时编译模板字符串的简单 demo：</p>

<div id="vue-compile-demo" class="demo"><textarea rows="10"></textarea> <div><label>render:</label> <pre><code>function anonymous() {
  with(this){return _c('div',[_m(0),(message)?_c('p',[_v(_s(message))]):_c('p',[_v("No message.")])])}
}</code></pre> <label>staticRenderFns:</label> <pre><code>_m(0): function anonymous() {
  with(this){return _c('header',[_c('h1',[_v("I'm a template!")])])}
}</code></pre> <!----></div></div>
<script>
new Vue({
  el: '#vue-compile-demo',
  data: {
    templateText: '\
<div>\n\
  <header>\n\
    <h1>I\'m a template!</h1>\n\
  </header>\n\
  <p v-if="message">\n\
    {{ message }}\n\
  </p>\n\
  <p v-else>\n\
    No message.\n\
  </p>\n\
</div>\
    ',
  },
  computed: {
    result: function () {
      if (!this.templateText) {
        return 'Enter a valid template above'
      }
      try {
        var result = Vue.compile(this.templateText.replace(/\s{2,}/g, ''))
        return {
          render: this.formatFunction(result.render),
          staticRenderFns: result.staticRenderFns.map(this.formatFunction)
        }
      } catch (error) {
        return error.message
      }
    }
  },
  methods: {
    formatFunction: function (fn) {
      return fn.toString().replace(/(\{\n)(\S)/, '$1  $2')
    }
  }
})
console.error = function (error) {
  throw new Error(error)
}
</script>
<style>
#vue-compile-demo {
  -webkit-user-select: inherit;
  user-select: inherit;
}
#vue-compile-demo pre {
  padding: 10px;
  overflow-x: auto;
}
#vue-compile-demo code {
  white-space: pre;
  padding: 0
}
#vue-compile-demo textarea {
  width: 100%;
  font-family: monospace;
}
</style>


  
  
    <div class="guide-links">
      
      
        <span>← <a href="/v2/guide/custom-directive.html">自定义指令</a></span>
      
      
      
        <span style="float: right;"><a href="/v2/guide/plugins.html">插件</a> →</span>
      
    </div>
  
  <div class="footer">
      <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div class="bsa-cpc"></div>
<script>
  (function(){
    if(typeof _bsa !== 'undefined' && _bsa) {
    _bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
      target: '.bsa-cpc',
      align: 'horizontal',
      disable_css: 'true'
    });
      }
  })();
</script>

    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/guide/render-function.md" target="_blank">
      在 GitHub 上编辑此页！
    </a>
  </div>
</div>
<{/block}>
